home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / kcl / akcl / kcl.lha / ild / ild.c next >
C/C++ Source or Header  |  1986-05-24  |  7KB  |  314 lines

  1. /*
  2.     ild: incremental linker/loader on System V
  3.  
  4.                 (C) Masami Hagiya, 1986
  5.  
  6.     Usage:
  7.         % ild loading_object start_address \
  8.               loaded_object output_file
  9.     
  10.     The start address is in decimal.
  11.  
  12.     How to Create:
  13.  
  14.     To compile this file (ild.c) and make ild, do the following.
  15.  
  16.         % cc -o ild ild.c -lld
  17.  
  18.     
  19.     Problem:
  20.     This program only supports the simplest linear search for symbols.
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <filehdr.h>
  26. #include <aouthdr.h>
  27. #include <scnhdr.h>
  28. #include <reloc.h>
  29. #include <syms.h>
  30. #include <storclass.h>
  31. #include <ldfcn.h>
  32.  
  33. struct filehdr my_header;
  34. struct syment *my_symbol_table;
  35. char *my_string_table;
  36.  
  37. struct filehdr header;
  38. struct scnhdr section[9];
  39. char *text;
  40. struct syment *symbol_table;
  41. char *string_table;
  42.  
  43. char *start_address;
  44.  
  45. struct reloc relocation_info;
  46.  
  47. main(argc, argv)
  48. int argc;
  49. char *argv[];
  50. {
  51.     if (argc < 5) {
  52.         fprintf(stderr, "Arg count.\n");
  53.         exit(1);
  54.     }
  55.     /*
  56.     printf("ild %s %s %s %s\n", argv[1], argv[2], argv[3], argv[4]);
  57.     fflush(stdout);
  58.     */
  59.     get_myself(argv[1]);
  60.     /*
  61.     printf("Myself got.\n");
  62.     fflush(stdout);
  63.     */
  64.     start_address = (char *)atoi(argv[2]);
  65.     fasload(argv[3], argv[4]);
  66.     exit(0);
  67. }
  68.  
  69. get_myself(filename)
  70. char *filename;
  71. {
  72.     int i;
  73.     LDFILE *ldptr;
  74.     extern char *malloc();
  75.  
  76.     ldptr = ldopen(filename, NULL);
  77.     if (ldptr == NULL) {
  78.         fprintf(stderr, "Can't open %s\n", filename);
  79.         exit(1);
  80.     }
  81.  
  82.     ldfhread(ldptr, &my_header);
  83.  
  84.     ldtbseek(ldptr);
  85.     my_symbol_table
  86.     = (struct syment *)malloc(sizeof(struct syment) * my_header.f_nsyms);
  87.     /*
  88.     sizeof(struct syment) and SYMESZ are not always the same.
  89.     */
  90.     for (i = 0;  i < my_header.f_nsyms;  i++)
  91.         FREAD(&my_symbol_table[i], SYMESZ, 1, ldptr);
  92.     /*
  93.     If the string table is not empty,
  94.     its length is stored after the symbol table,
  95.     This is not described in the manual, and may change in the future.
  96.     */
  97.     if (FREAD(&i, 4, 1, ldptr) > 0)    {
  98.         my_string_table = malloc(i);
  99.         FSEEK(ldptr, -4, 1);
  100.         FREAD(my_string_table, 1, i, ldptr);
  101.     }
  102.  
  103.     ldclose(ldptr);
  104. }
  105.  
  106. fasload(filename, outputfilename)
  107. char *filename, *outputfilename;
  108. {
  109.     register struct syment *sym, *end;
  110.     int i, n;
  111.     LDFILE *ldptr;
  112.     FILE *fp;
  113.  
  114.     extern char *malloc();
  115.  
  116.     ldptr = ldopen(filename, "r");
  117.     if (ldptr == NULL) {
  118.         fprintf(stderr, "Can't open %s\n", filename);
  119.         exit(1);
  120.     }
  121.  
  122.     ldfhread(ldptr, &header);
  123.     if (header.f_opthdr != 0) {
  124.         fprintf(stderr, "Unexpected optional header.\n");
  125.         exit(1);
  126.     }
  127.  
  128.     if (header.f_nscns < 3 || header.f_nscns > 8) {
  129.         fprintf(stderr, "Illegal number of sections.\n");
  130.         exit(1);
  131.     }
  132.  
  133.     for (i = 1;  i <= header.f_nscns;  i++)
  134.         ldshread(ldptr, i, §ion[i]);
  135.  
  136.     if (strcmp(section[1].s_name, ".text") != 0) {
  137.         fprintf(stderr, ".text not found.\n");
  138.         exit(1);
  139.     }
  140.     if (strcmp(section[2].s_name, ".data") != 0) {
  141.         fprintf(stderr, ".data not found.\n");
  142.         exit(1);
  143.     }
  144.     /*
  145.     The bss segment need not exist.
  146.     */
  147.     /*
  148.     if (strcmp(section[3].s_name, ".bss") != 0) {
  149.         fprintf(stderr, ".bss not found.\n");
  150.         exit(1);
  151.     }
  152.     */
  153.  
  154.     if (section[1].s_size > 0 &&
  155.         section[1].s_scnptr !=
  156.         sizeof(struct filehdr) +
  157.         header.f_nscns*sizeof(struct scnhdr)) {
  158.         fprintf(stderr, "Contradictory text start.\n");
  159.         exit(1);
  160.     }
  161.     if (section[1].s_size > 0 && section[2].s_size > 0 &&
  162.         section[1].s_scnptr + section[1].s_size !=
  163.         section[2].s_scnptr) {
  164.         fprintf(stderr, "Contradictory data start.\n");
  165.         exit(1);
  166.     }
  167.  
  168.     text = malloc(section[1].s_size + section[2].s_size);
  169.  
  170.     FSEEK(ldptr, section[1].s_scnptr, 0);
  171.     FREAD(text, 1, section[1].s_size + section[2].s_size, ldptr);
  172.  
  173.     ldtbseek(ldptr);
  174.     symbol_table
  175.     = (struct syment *)malloc(sizeof(struct syment) * header.f_nsyms);
  176.     /*
  177.     sizeof(struct syment) and SYMESZ are not always the same.
  178.     */
  179.     for (i = 0;  i < header.f_nsyms;  i++)
  180.         FREAD(&symbol_table[i], SYMESZ, 1, ldptr);
  181.     /*
  182.     If the string table is not empty,
  183.     its length is stored after the symbol table,
  184.     This is not described in the manual, and may change in the future.
  185.     */
  186.     if (FREAD(&i, 4, 1, ldptr) > 0)    {
  187.         string_table = malloc(i);
  188.         FSEEK(ldptr, -4, 1);
  189.         FREAD(string_table, 1, i, ldptr);
  190.     }
  191.  
  192.     end = symbol_table + header.f_nsyms;
  193.     for (sym = symbol_table; sym < end; sym++) {
  194.         switch (sym->n_scnum)    {
  195.         case 1: case 2: case 3:
  196.             /*
  197.             If the section number of the symbol is 1, 2, or 3,
  198.             the start address of the text is stored.
  199.             This is not described in the manual,
  200.             and may depend on CPU or may change in the future.
  201.             */
  202.             sym->n_value = (int)start_address;
  203.             break;
  204.         case N_UNDEF:
  205.             search_symbol(sym);
  206.             break;
  207.         default:
  208.             /*
  209.             Does nothing. Is it OK?
  210.             */
  211.             break;
  212.         }
  213.         sym += sym->n_numaux;
  214.     }
  215.  
  216.     ldrseek(ldptr, 1);
  217.     for (i = 0; i < section[1].s_nreloc; i++) {
  218.         /*
  219.         FREAD(&relocation_info, sizeof(struct reloc), 1, ldptr);
  220.         */
  221.         FREAD(&relocation_info, 10, 1, ldptr);
  222.         relocate();
  223.     }
  224.  
  225.     ldrseek(ldptr, 2);
  226.     for (i = 0; i < section[2].s_nreloc; i++) {
  227.         /*
  228.         FREAD(&relocation_info, sizeof(struct reloc), 1, ldptr);
  229.         */
  230.         FREAD(&relocation_info, 10, 1, ldptr);
  231.         relocate();
  232.     }
  233.  
  234.     fp = fopen(outputfilename, "w");
  235.     if (fp == NULL)    {
  236.         fprintf(stderr, "Can't creat %s.\n", outputfilename);
  237.         exit(1);
  238.     }
  239.     fwrite(&header, sizeof(struct filehdr), 1, fp);
  240.     for (i = 1;  i <= header.f_nscns;  i++)
  241.         fwrite(§ion[i], sizeof(struct scnhdr), 1, fp);
  242.     fwrite(text, 1, section[1].s_size + section[2].s_size, fp);
  243.  
  244.     fclose(fp);
  245.     ldclose(ldptr);
  246. }
  247.  
  248. search_symbol(sym)
  249. register struct syment *sym;
  250. {
  251.     register struct syment *p, *end;
  252.  
  253.     end = my_symbol_table + my_header.f_nsyms;
  254.     for (p = my_symbol_table; p < end; p++)    {
  255.         /*
  256.         Is the following check enough?
  257.         */
  258.         if (1 <= p->n_scnum && p->n_scnum <= 3 &&
  259.             p->n_sclass == C_EXT &&
  260.             (sym->n_zeroes == 0
  261.             ? (p->n_zeroes == 0 &&
  262.                strcmp(&my_string_table[p->n_offset],
  263.                   &string_table[sym->n_offset]) == 0)
  264.             : (p->n_zeroes != 0 &&
  265.                strncmp(p->n_name, sym->n_name, SYMNMLEN) == 0)))
  266.                 goto FOUND;
  267.         p += p->n_numaux;
  268.     }
  269.  
  270.     sym->n_name[SYMNMLEN] = '\0';
  271.     fprintf(stderr, "%s: undefined symbol.\n",
  272.         (sym->n_zeroes ? sym->n_name : &string_table[sym->n_offset]));
  273.     exit(1);
  274.  
  275. FOUND:
  276.     /*
  277.     Subtract the original value.
  278.     This is not described in the manual,
  279.     and I don't understand why.
  280.     */
  281.     sym->n_value = p->n_value - sym->n_value;
  282. }
  283.  
  284. relocate()
  285. {
  286.     char *where, *p;
  287.     int value;
  288.  
  289.     where = text + relocation_info.r_vaddr;
  290.     if (relocation_info.r_type == R_ABS)
  291.         return;
  292.     /*
  293.     The following code depends on CPU.
  294.     */
  295.     if (relocation_info.r_type != R_DIR32S) {
  296.         fprintf(stderr, "%d: unsupported relocation type.",
  297.             relocation_info.r_type);
  298.         exit(1);
  299.     }
  300.     /*
  301.     Assuming R_DIR32S.
  302.     */
  303.     p = (char *)(&value);
  304.     p[3] = where[0];
  305.     p[2] = where[1];
  306.     p[1] = where[2];
  307.     p[0] = where[3];
  308.     value += symbol_table[relocation_info.r_symndx].n_value;
  309.     where[0] = p[3];
  310.     where[1] = p[2];
  311.     where[2] = p[1];
  312.     where[3] = p[0];
  313. }
  314.